// pages/order/order.js
import Toast from '@vant/weapp/toast/toast';
import request from '../../utils/request';
var util = require("../../utils/util.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        option1: [
            { text: '单据类型', value: 0 },
            { text: '进货单', value: 1 },
            { text: '销售单', value: 2 },
          ],
        option2: [
            { text: '单据状态', value: 0 },
            { text: '待处理', value: 1 },
            { text: '待收款', value: 2 },
            { text: '已完成', value: 3 },
            { text: '已取消', value: 4 },
        ],
        state: 0,
        type: 0,
        keyWord: null,
        pageSize:10,
        pageNum: 1,
        active: 0,
        orderList:[],
        haveNext: true,
        isNull: false,
    },
    // 搜索框变更
    onChange(e) {
        this.setData({
            keyWord: e.detail,
        });
    },
    changeType(event) {
        this.setData({
            type: event.detail,
            pageNum: 1,
            orderList: []
        })
        this.getList();
    },
    changeState(event){
        this.setData({
            state: event.detail,
            pageNum: 1,
            orderList: []
        })
        this.getList();
    },
    // 搜索事件
    onSearch() {
        this.setData({
            pageNum: 1,
            orderList: []
        })
        this.getList();
    },
    toEdit(){
        console.log("add")
    },
    loadMore(event){
        let {haveNext,pageNum} = this.data
        this.setData({
            pageNum: pageNum+ 1
        })
        if (haveNext) {
            this.getList();
        }
    },
    getList(load){
        wx.showLoading({
            title:"加载中，请稍后"
        })
        let {state, type, keyWord, pageSize, pageNum} = this.data
        const para = {
            state, type ,keyWord, pageSize, pageNum
        }
        request("/smile/smileInvoice/getList",para,"POST").then(res => {
            if (res.records.length == 0) {
                this.setData({isNull: true})
                return;
            }else{
                let haveNext = res.current * res.size < res.total;
                let {orderList} = this.data;
                res.records.map(record =>{
                    record.updateTime = util.utc2bj(record.updateTime)
                    let initialValue = 0;
                    let totalPrice = record.line.reduce(function(accumulator, currentValue){
                        return accumulator + currentValue.productPrice * currentValue.productAmount;
                    }, initialValue);
                    record.totalPrice = totalPrice.toFixed(2);
                })
                orderList.push.apply(orderList,res.records);
                this.setData({haveNext,
                    isNull: false,
                orderList})
            }
        }).finally(() => wx.hideLoading())
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getTabBar().init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})